package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.TravelCommend;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class IndexController {
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ISearchService searchService;

    @RequestMapping("")
    public String index(Model model){
        //tcs,5篇推荐的游记
        List<TravelCommend> list = travelCommendService.listTop5();
        model.addAttribute("tcs",travelCommendService.listTop5());
        //scs,5篇推荐的攻略
        model.addAttribute("scs",strategyCommendService.listTop5());
        return "index/index";
    }

    @Reference
    private ITravelService travelService;
    @RequestMapping("/index/travelPage")
    public String travelPage(@ModelAttribute("qo")TravelQueryObject qo, Model model){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "index/travelPageTpl";
    }

    @RequestMapping("/q")
    public String search(@ModelAttribute("qo") SearchQueryObject qo,Model model){
        String viewName = null;
        switch (qo.getType()){
            case SearchQueryObject.SEARCH_DEST:
                viewName = "searchDest";
                searchDest(qo.getKeyword(),model);
                break;
            case SearchQueryObject.SEARCH_STRATEGY:
                viewName = "searchStrategy";
                searchStrategy(qo,model);
                break;
            case SearchQueryObject.SEARCH_TRAVEL:
                viewName = "searchTravel";
                searchTravel(qo,model);
                break;
            case SearchQueryObject.SEARCH_USER:
                viewName = "searchUser";
                searchUser(qo,model);
                break;
            default:
                viewName = "searchAll";
                searchAll(qo,model);
        }
        return "index/" + viewName;
    }

    private void searchAll(SearchQueryObject qo, Model model) {
        int total = 0;

        Page<StrategyTemplate> sts = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subtitle", "summary");
       total += sts.getTotalElements();
        Page<TravelTemplate> tts = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        total += tts.getTotalElements();
        Page<UserInfoTemplate> users = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class, qo,
                "nickName", "destName");
        total += users.getTotalElements();
        Page<DestinationTemplate> dests = searchService.hightLightSearch(DestinationTemplate.INDEX_TYPE_NAME, DestinationTemplate.class, qo, "name", "info");
        total += dests.getTotalElements();
        SearchResult data = new SearchResult();
        data.setTotal(total);
        data.setStrategys(sts.getContent());
        data.setTravels(tts.getContent());
        data.setUsers(users.getContent());
        data.setDests(dests.getContent());
        model.addAttribute("data",data);
    }

    private void searchUser(SearchQueryObject qo, Model model) {
        Page<UserInfoTemplate> page = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME, UserInfoTemplate.class, qo,
                "nickName", "destName");
        model.addAttribute("page",page);
    }

    private void searchTravel(SearchQueryObject qo, Model model) {
        Page<TravelTemplate> page = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME, TravelTemplate.class, qo,
                "title", "summary");
        model.addAttribute("page",page);
    }

    private void searchStrategy( SearchQueryObject qo, Model model) {
        Page<StrategyTemplate> page = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME, StrategyTemplate.class, qo,
                "title", "subtitle", "summary");
        model.addAttribute("page",page);
    }


    private void searchDest(String keyword, Model model) {
        SearchResult data = new SearchResult();

        List<StrategyTemplate> strategys = strategySearchService.listByDestName(keyword);
        List<TravelTemplate> travels = travelSearchService.listByDestName(keyword);
        List<UserInfoTemplate> users = userInfoSearchService.listByDestName(keyword);
        int num = strategys.size() + travels.size() + users.size();
        DestinationTemplate destination = destinationSearchService.getByName(keyword);
        int total = num == 0 ? 0 : num +1;
        data.setTotal(total);

        model.addAttribute("dest",destination);
        data.setStrategys(strategys);
        data.setTravels(travels);
        data.setUsers(users);
        model.addAttribute("data",data);
    }


  /*  @RequireLogin
    @RequestMapping("/1")
    @ResponseBody
    public Object index1(UserInfo userInfo){
        return userInfo;
    }

    @RequireLogin
    @RequestMapping("/2")
    @ResponseBody
    public Object index2(@LoginUser UserInfo userInfo){
        return userInfo;
    }

    @RequireLogin
    @RequestMapping("/3")
    @ResponseBody
    public Object index3(UserInfo userInfo){
        return userInfo;
    }*/
}
